package Demo02;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class Receive2 {

    public static void main(String[] args) throws IOException {
        MulticastSocket ms = new MulticastSocket(10086);
        InetAddress address = InetAddress.getByName("224.0.0.0");
        ms.joinGroup(address);
        //创建接收信息的数据包
        byte[] bytes=new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        ms.receive(dp);
        byte[] data = dp.getData();
        String name = dp.getAddress().getHostName();
        String ip = dp.getAddress().getHostAddress();
        int length = dp.getLength();
        System.out.println("ip为"+ip+",主机名为："+name+"的人，发送了数据："+new String(data,0,length));
        ms.close();
    }
}
